package com.ufgov.ip.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.ufgov.ip.entity.IpMenu;

public interface IMenuDao extends PagingAndSortingRepository<IpMenu, String>,JpaSpecificationExecutor<IpMenu>{
	
	//查询所有的一级菜单
	List<IpMenu> findMenuByLevelNum(String levelNum);
	
	//查询所有的一级并且显示的菜单
	List<IpMenu> findMenuByLevelNumAndIsShow(String levelNum,String isShow);
    
	 @Modifying(clearAutomatically = true) @Query("update IpMenu ipMenu set ipMenu.isShow=:isShow where ipMenu.menuId=:menuId")
	 void updateMenuByMenuId(@Param("menuId")String menuId, @Param("isShow")String isShow);

	//查询所有子级菜单
	List<IpMenu> findByParentMenuId(String parentMenuId);
	
	@Query("select max(func.menuId)+1 from IpMenu func")
	long getNextId();

	@Query(" from IpMenu order by levelNum, dispOrder asc")
	List<IpMenu> findMenuAll();

	IpMenu findByMenuName(String menuName);
	//模糊查询菜单名称
	@Query(" from IpMenu where menuName like :menuName order by levelNum, dispOrder asc")
	List<IpMenu> findMenuByMenuNameLike(@Param("menuName")String menuName);
	
	IpMenu findMenuByMenuName(String parentMenuName);

	IpMenu findByMenuId(String menuId);
	
	/**
	 * 通过父节点ID 更新父节点名称 
	 * @param parentMenuId
	 * @param parentMenuName
	 */
	 @Modifying(clearAutomatically = true) @Query("update IpMenu ipMenu set ipMenu.parentMenuName=:parentMenuName where ipMenu.parentMenuId=:parentMenuId")
	 void updateMenuByparentMenuId(@Param("parentMenuId")String parentMenuId, @Param("parentMenuName")String parentMenuName);

	@Query(" from IpMenu where levelNum='1' and (authLevel='1' or authLevel='2') order by levelNum, dispOrder asc")
	List<IpMenu> IpMenuListByHirer();
	@Query(" from IpMenu where levelNum = ?1 or levelNum = ?2 order by levelNum, dispOrder asc")
	List<IpMenu> findMenuForLevelNum(String levelNum1, String levelNum2);
	
	
}
